
package com.udc.medLine.factory;

import java.util.List;

import com.udc.medLine.entity.CondicaoVO;
import com.udc.medLine.entity.Cliente;
import com.udc.medLine.exception.CampoDuplicadoException;
import com.udc.medLine.persistence.ClienteDAO;

public class FactoryCliente {

	private ClienteDAO clienteDAO;

	/**
	 * 
	 */
	public FactoryCliente() {
		super();
		clienteDAO = new ClienteDAO();
		
		// TODO Stub de construtor gerado automaticamente
	}

	public Cliente getCliente() {
		return (clienteDAO.getCliente());
	}

	public void setCliente(Cliente new_cliente) {
		this.clienteDAO.setCliente(new_cliente);
	}

	public void insert() throws Exception {
		int size = clienteDAO.search(
				"From cliente in class Cliente WHERE cliente.cpf = '"
						+ clienteDAO.getCliente().getCpf()+"'").size();
		if (size > 0) {
			throw new CampoDuplicadoException("cpf");
		}
		
		size = clienteDAO.search(
				"From cliente in class Cliente WHERE cliente.email = '"
						+ clienteDAO.getCliente().getEmail() + "'").size();
		if (size > 0) {
			throw new CampoDuplicadoException("email");
		}

		size = clienteDAO.search(
				"From cliente in class Cliente WHERE cliente.rg = '"
						+ clienteDAO.getCliente().getRg() + "'").size();
		if (size > 0) {
			throw new CampoDuplicadoException("rg");
		}
		
		size = clienteDAO.search(
				"From cliente in class Cliente WHERE cliente.usuario = '"
						+ clienteDAO.getCliente().getUsuario()+"'").size();
		if (size > 0) {
			throw new CampoDuplicadoException("usuario");
		}
		clienteDAO.insert();
	}

	
	public void delete() throws Exception {
		int size = clienteDAO.search(
				"From cliente in class Cliente ,Venda as v WHERE v.cliente.id = "
						+ clienteDAO.getCliente().getId()).size();
        if (size > 0) {
			throw new CampoDuplicadoException("referencia");
		}
		clienteDAO.delete();
    }

	
	public void update() throws Exception {
		int size = clienteDAO.search(
				"From cliente in class Cliente WHERE cliente.cpf = '"
						+ clienteDAO.getCliente().getCpf()+"' and cliente.id != " + clienteDAO.getCliente().getId()).size();
		if (size > 0) {
			throw new CampoDuplicadoException("cpf");
		}
	
		size = clienteDAO.search(
				"From cliente in class Cliente WHERE cliente.rg = '"
						+ clienteDAO.getCliente().getRg()+"'and cliente.id != " + clienteDAO.getCliente().getId()).size();
					
		if (size > 0) {
			throw new CampoDuplicadoException("matricula");
		}
		
		size = clienteDAO.search(
				"From cliente in class Cliente WHERE cliente.email = '"
						+ clienteDAO.getCliente().getEmail()+"'and cliente.id != " + clienteDAO.getCliente().getId()).size();
					
		if (size > 0) {
			throw new CampoDuplicadoException("matricula");
		}
		
		
		size = clienteDAO.search(
				"From cliente in class Cliente WHERE cliente.usuario = '"
						+ clienteDAO.getCliente().getUsuario()+"'and cliente.id != " + clienteDAO.getCliente().getId()).size();
		if (size > 0) {
			throw new CampoDuplicadoException("usuario");
		}
		clienteDAO.update();
	}

	
	public List getCliente(String campo, String valor){
		List lista;
		try {
			lista = clienteDAO.search("From cliente in class Cliente WHERE "+campo+" ='"+ valor + "'");
		}
		catch (Exception e){
			return null;
		}
		return lista;
	}
	

	public List getListaClientes(){		
		List lista;	
		try{		
			lista = clienteDAO.search("From cliente in class Cliente");
		}
		catch (Exception e){
		 	return null;
		}
		return lista;		
	}
	
	public Cliente obterCliente() throws Exception {
		return clienteDAO.retrive();
	}

	public List search(CondicaoVO condicao) throws Exception {
       String hql ;
       if (condicao.getValor().equals("")){
			hql = "From cliente in class Cliente";
		}else {
			hql = "From cliente in class Cliente WHERE ";
		if (condicao.getCampo().equals("id")) {
			hql += "id";
			if (condicao.getOperador().equals("contem")) {
				condicao.setOperador("igual");
			}
	   
	    } else if(condicao.getCampo().equals("usuario")) {
			hql += "usuario";
			condicao.setValor("'" + condicao.getValor() + "'");
		}
		else if(condicao.getCampo().equals("nome")) {
		   hql += "nome";
		}
		else if(condicao.getCampo().equals("cpf")) {
			   hql += "cpf";
		}
		else if(condicao.getCampo().equals("rg")) {
			   hql += "rg";
		}
		
		if (condicao.getOperador().equals("igual")) {
			hql += "=";
		} else if (condicao.getOperador().equals("diferente")) {
			hql += "!=";
		} else if (condicao.getOperador().equals("maior")) {
			hql += ">";
		} else if (condicao.getOperador().equals("menor")) {
			hql += "<";
		} else if (condicao.getOperador().equals("contem")) {
			hql += " like  ";
			condicao.setValor("('%" + condicao.getValor() + "%')");
		}
		}
		hql += condicao.getValor();
		return clienteDAO.search(hql);
	}
	
	public List searchCriteria(CondicaoVO condicao) throws Exception {
	     return clienteDAO.searchCriterio(condicao);
	}
}
